package com.example.demo.leetcode.top100;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _30两两交换链表中的节点
 * @description
 * @date 2023/06/11 11:05
 * <p>
 * ******************************************************
 */
public class _30两两交换链表中的节点 {

    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode dummy1 = dummy;

        while (dummy.next != null && dummy.next.next != null) {
            ListNode next = dummy.next.next.next;
            ListNode first = dummy.next;
            ListNode second = dummy.next.next;

            dummy.next = second;
            second.next = first;
            first.next = next;
            dummy = first;
        }
        return dummy1.next;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
